import util from '@/libs/util'
import { validatenull } from '@/libs/validate'
import dayjs from 'dayjs'
const type = 'session'

export default {
  namespaced: true,
  state: {
    access_token: util.storage.getStorage({
      name: 'access_token'
    }) || '',
    refresh_token: util.storage.getStorage({
      name: 'refresh_token'
    }) || '',
    expires_in: util.storage.getStorage({
      name: 'expires_in'
    }) || '',
    open_keep_online: util.storage.getStorage({
      name: 'open_keep_online'
    }) || '',
    callback_status: util.storage.getStorage({
      name: 'callback_status'
    }) || false
  },
  mutations: {
    SET_ACCESS_TOKEN: (state, access_token) => {
      state.access_token = access_token
      util.storage.setStorage({
        name: 'access_token',
        content: state.access_token,
        type: type
      })
    },
    SET_REFRESH_TOKEN: (state, refresh_token) => {
      state.refresh_token = refresh_token
      util.storage.setStorage({
        name: 'refresh_token',
        content: state.refresh_token,
        type: type
      })
    },
    SET_EXPIRES_IN: (state, expires_in) => {
      if (!validatenull(expires_in)) {
        const timestamp = dayjs().valueOf()
        expires_in = expires_in * 1000 + timestamp
        // console.log('timestamp', timestamp)
        // console.log('expires_in', expires_in)
        // console.log('date', dayjs(timestamp).format('YYYY-MM-DD HH:mm:sss'))
      }
      state.expires_in = expires_in
      util.storage.setStorage({
        name: 'expires_in',
        content: state.expires_in,
        type: type
      })
    },
    SET_OPEN_KEEP_ONLINE: (state, open_keep_online) => {
      state.open_keep_online = open_keep_online
      util.storage.setStorage({
        name: 'open_keep_online',
        content: state.open_keep_online,
        type: type
      })
    },
    SET_CALLBACK_STATUS: (state, callback_status) => {
      state.callback_status = callback_status
      util.storage.setStorage({
        name: 'callback_status',
        content: state.callback_status,
        type: type
      })
    }
  },
  actions: {
  },
  getters: {
    refresh_token: (state) => {
      return state.refresh_token
    },
    access_token: (state) => {
      return state.access_token
    },
    expires_in: (state) => {
      return state.expires_in
    },
    open_keep_online: (state) => {
      return state.open_keep_online
    },
    callback_status: (state) => {
      return state.callback_status
    }
  }
}
